package com.ubs.lingua.buildtool.cs.utils.ant;

import java.io.File;

import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.Zip;

import com.ubs.lingua.buildtool.cs.parameters.BuildParameters;

/**
 * Wrapper for the Apache Ant ZIP task.
 */
public final class Packer extends Zip {

    /**
     * UTF-8 encoding name
     */
    private static final String ENC_UTF8 = "UTF-8";
    /**
     * ISO 8859-1 (Latin Alphabet No. 1) encoding name
     */
    private static final String ENC_ISO8859_1 = "ISO-8859-1";

    /**
     * Private constructor which setup the mock Ant project and adds the {@link AntLogListener} to it
     */
    private Packer() {
        super();

        // creating mock project
        final Project prj = new Project();
        prj.setName("Mock Packer project");
        prj.addBuildListener(new AntLogListener(LogFactory.getLog(Packer.class)));
        setProject(prj);

        setTaskType("zip");
        setTaskName("zip");

        // creating mock target
        final Target target = new Target();
        target.setName("Mock Packer target");
        setOwningTarget(target);
    }

    /**
     * Creates a zipfile.
     * @param baseDir the reference directory from where to zip
     * @param destFile the zip-file to create
     * @param settings the predefined configurations of the ZIP packer
     * @see com.ubs.lingua.buildtool.cs.parameters.BuildParameters.PackerSettings
     */
    public static void antZip(final File baseDir, final File destFile, final BuildParameters.PackerSettings settings) {
        final Packer packer = new Packer();
        packer.setBasedir(baseDir);
        packer.setDestFile(destFile);
        packer.setLevel(9);

        switch (settings) {
        case JAVA: {
            // java.util.zip is the main consumer.
            packer.setEncoding(ENC_UTF8);
            packer.setUseLanguageEncodingFlag(true);
            break;
        }
        case MAXIMUM_INTEROP: {
            // Maximum interop settings. Archive should be extracted correctly by java.util.zip, 7Zip, WinZIP, PKWARE tools and most likely InfoZIP tools.
            // Will be unusable with Windows' "compressed folders" feature and bigger than archives without the unicode extra fields.
            packer.setEncoding(ENC_UTF8);
            packer.setUseLanguageEncodingFlag(true);
            final UnicodeExtraField unicodeExtraField = new UnicodeExtraField();
            unicodeExtraField.setValue("always");
            packer.setCreateUnicodeExtraFields(unicodeExtraField);
            break;
        }
        case WINDOWS: {
            packer.setEncoding(ENC_ISO8859_1);
            final UnicodeExtraField unicodeExtraField = new UnicodeExtraField();
            unicodeExtraField.setValue("always");
            packer.setCreateUnicodeExtraFields(unicodeExtraField);
            break;
        }
        default: {
            // Java
            packer.setEncoding(ENC_UTF8);
            packer.setUseLanguageEncodingFlag(true);
            break;
        }
        }

        AntLauncher.launchTask(packer);
    }

}
